<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Send_edm extends CI_Controller {

	function __construct() {
		parent::__construct();
	}

	public function index() {
		//$this->load->helper('email');
		$html = $this -> init("發EDM", "jquery", "form");
		$this -> load -> model('client_model');

		if (empty($id)) {
			$id = $_GET["id"];
		}

		$ids = explode(',', $id);
		$emails = "";

		foreach ($ids as $_flg => $_id) {

			$client_query = $this -> client_model -> get_client2($_id);
			$client = $client_query -> row();

			//print_r($client);
			//if(!empty($client->email))
			//$emails.= $client->email;

			//$client_query = $this->client_model->get_client2($id);
			//$email="";
			//$html["client"]=$client_query->row();

			//echo $id;
			//print_r($html["client"]);

			if (!empty($client -> email) && $client -> send_email == "Y") {
				$emails .= $client -> email . ',';
			}

			if (!empty($client -> email1) && $client -> send_email1 == "Y") {

				$emails .= $client -> email1 . ',';
			}

			if (!empty($client -> email2) && $client -> send_email2 == "Y") {

				$emails .= $client -> email2 . ',';
			}
		}

		//if($id=="")
		//	$html["edm"]=$this->session->flashdata('flashdata_to');
		//else
		if (!empty($emails))
			$html["edm"] = $emails;

		$this -> load -> model('users_model');
		$html["users"] = $this -> users_model -> get_user_info($this -> login_lib -> users_id());
		$this -> load -> view('send/send_edm', $html);

	}

	public function group() {
		$email = $_GET["c"];
		$html = $this -> init("發EDM", "jquery", "form");
		$html["edm"] = $email;
		$this -> load -> model('users_model');
		$html["users"] = $this -> users_model -> get_user_info($this -> login_lib -> users_id());
		$this -> load -> view('send/send_edm', $html);
	}

	function email() {
		$html = $this -> init("郵件日志", "jquery", "table");
		$this -> load -> model('edm_model');
		$where = "";
		$config['per_page'] = 15;
		$config['total_rows'] = $this -> edm_model -> get_edm_count($where);

		$config['url'] = '/client/email_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['query_condition'] = "";
		$html['page'] = $config;
		$html['sequence'] = "";
		$html["result"] = $this -> edm_model -> get_edm($where, $limit);
		//echo $this->db->last_query();die();
		$this -> load -> view('send/email', $html);
	}

	public function test() {
		
		//phpinfo();
		
		/*
		if(mail("nvizero@yahoo.com.tw", 'imissu', "iloveu", 'really')){
			
			echo 'was sended!!!!!!! ';
			mail("withvictor@gmail.com", 'imissu', "iloveu", 'really');
			
			$Subject="i miss u";
			$this -> send_mails("nvizero@yahoo.com.tw", 'ikaki', $Subject, 'miss u', 'really!?11');
		}else{
			echo 'error send!!';
		}
		*/

	}

	public function send_mails($mailaddr, $title, $Subject, $name, $content) {

		$this -> load -> library('mail/phpmailer');

		$mail = new PHPMailer();

		$is_gmail = false;

		$mail -> Debugoutput = 'html';
		//Set the hostname of the mail server
		$mail -> Host = 'smtp.gmail.com';
		//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
		$mail -> Port = 465;
		//Set the encryption system to use - ssl (deprecated) or tls
		$mail -> SMTPSecure = 'ssl';
		//Whether to use SMTP authentication
		$mail -> SMTPAuth = true;
		$mail -> CharSet = "utf-8";
		//郵件編碼
		//Username to use for SMTP authentication - use full email address for gmail
		$mail -> Username = "withvictor@gmail.com";
		//Password to use for SMTP authentication
		$mail -> Password = "2uuiajgialmy0417";
		//Set who the message is to be sent from
		$mail -> setFrom($mailaddr, $name);
		//Set an alternative reply-to address
		//$mail->addReplyTo($mailaddr, $name);
		//Set who the message is to be sent to
		$mail -> addAddress($mailaddr, $name);
		//Set the subject line
		$mail -> Subject = $Subject;
		//Read an HTML message body from an external file, convert referenced images to embedded,
		//convert HTML into a basic plain-text alternative body
		$iconv_content1 = mb_convert_encoding($content.base_url(), "Big5", "UTF8");
		$mail -> msgHTML($iconv_content1);
		//Replace the plain text body with one created manually
		//$mail->AltBody = 'This is a plain-text message body';

		$mail -> Send();
		$mail -> ClearAddresses();

	}

	function email_ajax() {
		$this -> load -> model('edm_model');
		$where = "";
		$config['per_page'] = 15;
		$config['total_rows'] = $this -> edm_model -> get_edm_count($where);

		$config['url'] = '/client/email_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['query_condition'] = "";
		$html['page'] = $config;
		$html['sequence'] = "";
		$html["result"] = $this -> edm_model -> get_edm($where, $limit);
		$this -> load -> view('email_ajax', $html);
	}

	public function send_edm_group_session() {

		session_start();

		//print_r($_SESSION["client_page"]);

		$c = "";

		$client_id = array();

		//if ($_SESSION["client_page"]) {

		foreach ($_SESSION["client_page"] as $key => $val) {
			//echo $key .'=>'. $val.'<br>';
			if ($val == 1) {
				$mail = $this -> get_member_mail($key);

				if ($c == "") {
					$c .= $mail;
				} else {
					$c .= "," . $mail;
				}
			}

		}

		//}
		if (!empty($_SESSION)) {
			session_unset($_SESSION["client_page"]);
			//session_destroy();
		}

		echo $c;
		//redirect("/send_edm/group?c=".$c);

	}

	public function send_edm_group() {

		$clients = $this -> input -> post('client');
		$c = "";

		$client_id = array();

		if ($clients) {
			foreach ($clients as $key => $val) {

				$mail = $this -> get_member_mail($val);

				if ($c == "") {
					$c .= $mail;
				} else {
					$c .= "," . $mail;
				}
			}
		}
		//echo $c;
		//if ($c) {

		//$this->session->set_flashdata('flashdata_to', $c);
		redirect("/send_edm/group?c=" . $c);

		//}

	}

	public function send_submit() {
		$this -> load -> model('edm_model');
		$data['to'] = $this -> input -> post('to');
		$data['sender'] = $this -> input -> post('sender');
		$data['name'] = $this -> input -> post('name');
		$data['subject'] = $this -> input -> post('subject');
		$data['content'] = $this -> input -> post('content');
		$data['status'] = 'N';
		$return_result = $this -> edm_model -> add($data);
		//echo $this->db->last_query();die();
		$this -> mail_test($data['to'], $data['content']);
		if ($return_result > 0) {
			//$this->load->library('email');
			//$config['protocol'] = 'smtp';
			//$config['mailpath'] = '/usr/sbin/sendmail';
			//$config['smtp_host']= "smtp.163.com";// SMTP Server.  Example: mail.earthlink.net
			//$config['smtp_user']= "13631275628@163.com";		// SMTP Username
			//$config['smtp_pass']= "ihwgg582624";		// SMTP Password
			//$config['smtp_port']= "25";		// SMTP Port
			$this -> load -> library('email');

			$this -> email -> from($data['sender'], $data['name']);

			$this -> email -> from($data['sender'], $data['name']);
			$this -> email -> to($data['sender']);
			//$this->email->cc('another@another-example.com');
			$this -> email -> bcc($data['to']);
			$this -> email -> subject($data['subject']);

			$iconv_content1 = @iconv("UTF-8", "Big-5", $data["content"]);

			$this -> email -> message($data["content"]);

			if ($this -> email -> send()) {
				$data['id'] = $return_result;
				$data['status'] = 'Y';
				$this -> edm_model -> update_status($data);
				//echo $this->db->last_query();
				//echo $this->email->print_debugger();
				redirect("/send_edm/email");
			}
		} else {
			echo "插入失敗";
		}

	}

	public function mail_test($content, $mail_addr) {

		$this -> load -> library('mail/phpmailer');
		$mail = new PHPMailer();
		// Set PHPMailer to use the sendmail transport
		$mail -> isSendmail();
		$mail -> CharSet = "Big-5";
		//郵件編碼
		//Set who the message is to be sent from
		$mail -> setFrom('from@example.com', 'First Last');
		//Set an alternative reply-to address
		$mail -> addReplyTo($mail_addr, 'First Last');
		//Set who the message is to be sent to
		//$mail->addAddress('nvizero@yahoo.com.tw', 'John Doe');
		//Set the subject line
		$mail -> Subject = 'PHPMailer sendmail test';
		//Read an HTML message body from an external file, convert referenced images to embedded,
		//convert HTML into a basic plain-text alternative body
		$mail -> msgHTML($content);
		//Replace the plain text body with one created manually
		$mail -> AltBody = 'This is a plain-text message body';
		//Attach an image file
		//$mail->addAttachment('images/phpmailer_mini.gif');

		//send the message, check for errors
		if (!$mail -> send()) {
			echo "Mailer Error: " . $mail -> ErrorInfo;
		} else {
			echo "Message sent!";
		}

	}

	public function get_member_mail($id) {

		$this -> load -> model('client_model');
		$html["client"] = $this -> client_model -> get_client_info($id);
		$email = "";

		if ($html["client"] -> email != "" && $html["client"] -> send_email == "Y") {
			$email .= $html["client"] -> email;
		}

		if ($html["client"] -> email1 != "" && $html["client"] -> email1 != "0" && $html["client"] -> send_email1 == "Y") {

			if (!empty($email)) {
				$email .= " , ";
			}

			$email .= $html["client"] -> email1;
		}

		if ($html["client"] -> email2 != "" && $html["client"] -> email2 != "0" && $html["client"] -> send_email2 == "Y") {

			if (!empty($email)) {
				$email .= " , ";
			}

			$email .= $html["client"] -> email2;
		}

		return $email;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
